<?php
/* OpenBIBLIO -- A library administration web interface
 * Copyright (C) 2002-2005 Fr&eacute;d&eacute;ric Descamps <lefred@tiscali.be>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */
include_once("includes/db.inc");
include_once("includes/functions.inc");

session_start();
include_once("includes/session.inc");

ControleAcces("histoel.php",$login,$pwd,$user_id,session_id());

if(!$user_id)
{
 exit;
}

$result = biblio_db_query("SELECT login FROM admin WHERE id = $user_id" ,$db);
$myrow = biblio_db_fetch_array($result);
$user = $myrow['login'] ;

$title = _("Historique des livres");
include ("includes/header.inc");
if ($user)
{

if ($_GET['id']) $id=$_GET['id'];
if($_GET['classe_q']) $classe_q=$_GET['classe_q'];

if(!$id)
{       if(!($classe_q)) 
        {
                $requete = "select * from classes order by classe desc";
                if(!($resultat = biblio_db_query($requete)))
                { print "PROBLEME : " .biblio_db_error(); }
                else 
                {       while ($row = biblio_db_fetch_array($resultat)) 
                        {
                          $classe_q = $row['id'];
                        }
                }
        }        
        $requete = "select t1.id, t1.nom, t1.prenom, t1.classe_id, t2.classe from eleves as t1 inner join classes as t2 on t2.id = t1.classe_id where t1.classe_id=" . $classe_q ." and t1.del=0 order by classe, nom, prenom ";
        if(!($resultat = biblio_db_query($requete)))
        { print _("PROBLEME : ") .biblio_db_error(); }
        else 
        {        
                echo "<br><CENTER><H3>";
		echo _("S&eacute;lectionner un &eacute;l&egrave;ve :");
		echo "</H3>";
                $requete2 = "select * from classes order by classe";
                if(!($resultat2 = biblio_db_query($requete2)))
                { print "PROBLEME : " .biblio_db_error(); }
                else 
                {
                  while ($row2 = biblio_db_fetch_array($resultat2)) 
	          {	          
	           echo " <A HREF=\"histoel.php?classe_q=". $row2['id'] . "\">" . $row2['classe'] . "</A> ";
	          }
                }       
                
                echo "<BR><BR><TABLE><tr><th>";
		echo _("Nom");
		echo "</th><th>";
		echo _("Pr&eacute;nom");
		echo "</th><th>";
		echo _("Classe");
		echo "</th><th>";
		echo _("Action");
		echo "</th></tr>";
                while ($row = biblio_db_fetch_array($resultat)) 
	        {      
                        $eleve_id = $row['id'];           
                        $eleve_nom =  $row['nom']; 
                        $eleve_prenom =  $row['prenom']; 
                        $eleve_classe =  $row['classe']; 
                        $eleve_classe_id =  $row['classe_id'];
                        echo "<TR><TD>$eleve_nom</TD><TD>$eleve_prenom</TD><TD>$eleve_classe</TD><TD ALIGN=RIGHT><A href=\"histoel.php?id=$eleve_id&nom=$eleve_nom&prenom=$eleve_prenom&classe_id=$eleve_classe_id\"><IMG SRC=\"images/wh_r_arr.gif\" ALT=\"";
			echo _("choisir");
			echo "\" border=0></A></TD>";            
                }
                echo "</TABLE></CENTER>";
        }
}
else 
{

         echo "<CENTER><H3>";
	 echo _("Voici l'historique de l'&eacute;l&egrave;ve : ");
	 echo "<i>$prenom $nom</i></H3>";
         $requete = "SELECT t1.date_out, t1.date_int, t3.etat etat_out, t6.etat etat_in, t1.id id_to, t2.nom as titre FROM prets as t1 inner join livres as t2 on t2.id = t1.livre_ID inner join etats as t3 on t3.id = t1.etat_out_ID left join etats as t6 on t6.id = t1.etat_in_ID where t1.eleve_id = $id  order by date_out, t1.id"; 
         put_log(7,"$prenom $nom",$user);
         if(!($resultat2 = biblio_db_query($requete)))
	 { print _("PROBLEME") . " $requete: " .biblio_db_error(); }
	 else 
 	 {  
	        $i=0;	        
	        echo "<TABLE><tr><th>";
		echo _("Titre");
		echo "</th><th>";
		echo _("Date de sortie");
		echo "</th><th>";
		echo _("Date de retour");
		echo "</th><th>";
		echo _("Nb. Jours<BR>emprunt&eacute;");
		echo "</th><th>";
		echo _("Etat Sortant");
		echo "</th><th>";
		echo _("Etat Entrant");
		echo "</th></tr>";
	        while ($myrow = biblio_db_fetch_array($resultat2)) 
	        {
		       $tab_mois = array('',_('janvier'),_('f&eacute;vrier'),_('mars'),_('avril'),_('mai'),_('juin'),_('juillet'),
		       	_('ao&ucirc;t'),_('septembre'),_('octobre'),_('novembre'),_('decembre'));
	               $date_sort = $myrow['date_out'];
		       $date_start = $date_sort;
	               ereg("(....)-(..)-(..)$",$date_sort,$match);
	               $jour  = $match[3];
	               $mois  = sprintf("%d",$match[2]);
	               $annee = $match[1];
	               $date_sort =  "$jour " . $tab_mois[$mois] . " $annee";
     		       $date_ent = $myrow['date_int'];
		       if ($date_ent == "") 
		       {
                        	$date_ent = _("Toujours en pr&ecirc;t");
				$date_end =  date("Y-m-d");
			} 
 	               else 
 		       {	                                      
			   $date_end =  $date_ent;
	         	   ereg("(....)-(..)-(..)$",$date_ent,$match);
	          	   $jour  = $match[3];
	           	   $mois  = sprintf("%d",$match[2]);
	            	   $annee = $match[1];
		       	   $date_ent = "$jour " . $tab_mois[$mois] . " $annee";
              		}   
                       $elementsdate = explode("-",$date_start);
                       $date_start = mktime(0,0,0,$elementsdate[1],$elementsdate[2],$elementsdate[0]);
		       $elementsdate = explode("-",$date_end);
                       $date_end = mktime(0,0,0,$elementsdate[1],$elementsdate[2],$elementsdate[0]);
                       $total_jour = $date_end - $date_start;
 		       $total_jour = $total_jour/86400;
		       $etat_out = $myrow['etat_out'];
		       $etat_in = $myrow['etat_in'];
	               $id_to = $myrow['id_to'];
	               $livre_titre = $myrow['titre'];;
		       $eleve = $myrow['prenom_el'] . " " . $myrow['nom_el'];

	               $i++;
	               if ($i%2 == 1) {$col="#DFE3EF";}
	               else {$col="lightyellow";}
                       echo "<TR bgcolor=$col><TD> $livre_titre</TD><TD>$date_sort</TD><TD>$date_ent</TD><TD ALIGN=RIGHT>$total_jour</TD><TD>$etat_out</TD><TD>$etat_in</TD>";
                }
		echo "</TABLE></center>";
         }              	   
}

}
?>
<br><br>
<div>
<span id=left-text>
<A HREF="admin.php?menu=1">
<?php echo _("retour menu administration"); ?>
</A><BR>
<A HREF="index.php">
<?php echo _("retour menu principal"); ?>
</A></span>
</div>
<br>
<?php
include("includes/footer.inc");
?>
